package com.ks.m.controller;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ks.dto.Product2Dto;
import com.ks.dto.ResultDto;
import com.ks.dto.UserInfoDto;
import com.ks.pc.service.CommonService;
import com.ks.pc.service.PcService;

import net.chinahrd.utils.CollectionKit;

@Controller
@RequestMapping("/m")
public class MController {
	@Autowired
	private PcService pcService;
	@Autowired
	private CommonService commonService;

	/**
	 * 移动端-主页-模糊查询产品
	 * 
	 * @param model
	 * @param searchTxt
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/searchTxt", method = RequestMethod.POST)
	public Map<Integer, List<Product2Dto>> searchTxt(Model model, String searchTxt) {
		if (searchTxt.length() == 0) {
			return null;
		}
		List<Product2Dto> list = CollectionKit.newList();
		Map<String, Object> param = CollectionKit.newMap();
		param.put("searchTxt", searchTxt);
		list = commonService.queryProductBySearchTxt(param);
		Map<Integer, List<Product2Dto>> rs = commonService.groupByShowIndexClassify(list);
		return rs;
	}

	/**
	 * 在线办理
	 * 
	 * @param userName
	 * @param userPhoneNum
	 * @param userIDCard
	 * @param userStages
	 * @param userNote
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/insertOnlineOpt", method = RequestMethod.POST)
	public ResultDto<String> insertOnlineOpt(String valStrs) {
		String[] val = valStrs.split(",");
		UserInfoDto dto = new UserInfoDto();
		switch (val.length) {
		case 4:
			dto.setUserName(val[0]);
			dto.setUserPhoneNum(val[1]);
			dto.setUserIDCard(val[2]);
			dto.setUserStages(val[3]);
			break;
		case 5:
			dto.setUserName(val[0]);
			dto.setUserPhoneNum(val[1]);
			dto.setUserIDCard(val[2]);
			dto.setUserStages(val[3]);
			dto.setUserNote(val[4]);
			break;
		}
		ResultDto<String> result = new ResultDto<String>();
		result.setType(pcService.saveOnlineOpt(dto));
		return result;
	}
}
